package com.zhudi.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhudi.model.entity.Team;
import com.zhudi.model.entity.User;
import com.zhudi.model.vo.TeamVO;
import com.zhudi.model.vo.UserVO;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanChangeUtils {

    //todo 考虑怎么将这个方法写的更通用
    public static Page<TeamVO> PageTeamToTeamVO(Page<Team> teamPage){
        Page<TeamVO> teamVOResult = new Page<>();
        BeanUtils.copyProperties(teamPage,teamVOResult);
        // 将Page<Team>转换为Page<TeamVO>
        List<TeamVO> teamVOList = teamPage.getRecords().stream()
                .map(team -> {
                    TeamVO teamVO = new TeamVO();
                    BeanUtils.copyProperties(team, teamVO);
                    return teamVO;
                })
                .collect(Collectors.toList());

        teamVOResult.setRecords(teamVOList);
        return teamVOResult;
    }

    public static Page<UserVO> PageUserToUserVO(Page<User> userPage){
        Page<UserVO> userVOResult = new Page<>();
        BeanUtils.copyProperties(userPage, userVOResult);
        // 将Page<User>转换为Page<UserVO>
        List<UserVO> userVOList = userPage.getRecords().stream()
                .map(user -> {
                    UserVO userVO = new UserVO();
                    BeanUtils.copyProperties(user, userVO);
                    return userVO;
                })
                .collect(Collectors.toList());

        userVOResult.setRecords(userVOList);
        return userVOResult;
    }
}
